package cn.daniel.framework.controller;

import cn.daniel.framework.core.web.WrapperResponse;
import cn.daniel.framework.dto.IPBlackWhiteListDTO;
import cn.daniel.framework.service.IPBlackWhiteListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author daniel.tuen
 * @date 2025/9/4 15:08
 * @description
 */
@RestController
@RequestMapping("/ipBlackWhiteList")
@Slf4j
public class IPBlackWhiteListController {

    @Autowired
    private IPBlackWhiteListService ipBlackWhiteListService;

    @GetMapping("/item")
    public WrapperResponse<IPBlackWhiteListDTO> getIPBlackWhiteList(@RequestParam long id) {
        log.debug("请求参数：id={}", id);
        IPBlackWhiteListDTO result = ipBlackWhiteListService.get(id);
        return WrapperResponse.success(result);
    }

    /**
     * 获取一页用户数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public WrapperResponse<List<IPBlackWhiteListDTO>> getPage(int pageNo, int pageSize) {
        List<IPBlackWhiteListDTO> list = ipBlackWhiteListService.getPage(pageNo, pageSize);
        return WrapperResponse.success(list);
    }

    @PostMapping("/save")
    public WrapperResponse<Long> save(@RequestBody IPBlackWhiteListDTO ipBlackWhiteListDTO) {

        if (ipBlackWhiteListDTO == null) {
            return WrapperResponse.error(234145, "上传数据为空", null);
        }

        if (ipBlackWhiteListDTO.getIpAddress() == null) {
            return WrapperResponse.error(231234, "地址不能为空", null);
        }

        IPBlackWhiteListDTO result = ipBlackWhiteListService.add(ipBlackWhiteListDTO);
        Long id = result.getId();
        return WrapperResponse.success( "成功", id);
    }
}
